package com.rnd.kx.mvp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rnd.kx.mvp.R;
import com.rnd.kx.mvp.bean.IndexBean;
import com.rnd.kx.mvp.utils.RangleTransForm;
import com.squareup.picasso.Picasso;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<IndexBean.ArticleBean> list;
    private Context context;
    private View headView;

    //普通布局
    private final int TYPE_ITEM1=1;
    private final int TYPE_ITEM2=2;

    public OnItemClickListener mOnItemClickListener;
    private int play = 0;

    public IndexArticleAdapter(List<IndexBean.ArticleBean> list, Context context) {
        this.list = list;
        this.context = context;
    }


    @Override
    public int getItemViewType(int position) {
        if(list.get(position).getType().equals("2")){//视频
            return TYPE_ITEM1;
        }else {
            return TYPE_ITEM2;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TYPE_ITEM1){
            View view = LayoutInflater.from(context).inflate(R.layout.item_index_article1, viewGroup, false);
            return new ViewHolder1(view);
        }else if (i == TYPE_ITEM2){
            View view = LayoutInflater.from(context).inflate(R.layout.item_index_article2, viewGroup, false);
            return new ViewHolder2(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ViewHolder1){//视频
            ((ViewHolder1) viewHolder).layout_re.setTag(i);
            ((ViewHolder1) viewHolder).title.setTag(i);
            IndexBean.ArticleBean articleBean = list.get(i);
            if (articleBean.getTitle() != null && !TextUtils.isEmpty(articleBean.getTitle())){
                ((ViewHolder1) viewHolder).title.setText(articleBean.getTitle());
            }

            if (articleBean.getTransmit_num() != null && !TextUtils.isEmpty(articleBean.getTransmit_num())){
                ((ViewHolder1) viewHolder).tv_share.setText(articleBean.getTransmit_num());
            }

            if (articleBean.getComment_num() != null && !TextUtils.isEmpty(articleBean.getComment_num())){
                ((ViewHolder1) viewHolder).tv_comment.setText(articleBean.getComment_num());
            }

            if (articleBean.getNice_num() != null && !TextUtils.isEmpty(articleBean.getNice_num())){
                ((ViewHolder1) viewHolder).tv_like.setText(articleBean.getNice_num());
            }

            TxVideoPlayerController controller = new TxVideoPlayerController(context);
            ((ViewHolder1) viewHolder).setController(controller);
            ((ViewHolder1) viewHolder).bindData(articleBean);
            if (play == i){
                return;
            }else {
                ((ViewHolder1) viewHolder).player.pause();
                //((ViewHolder1) viewHolder).player.release();
            }
        }else if (viewHolder instanceof ViewHolder2){//图文
            viewHolder.itemView.setTag(i);
            IndexBean.ArticleBean articleBean = list.get(i);
            if (articleBean.getTitle() != null && !TextUtils.isEmpty(articleBean.getTitle())){
                ((ViewHolder2) viewHolder).title.setText(articleBean.getTitle());
            }
            if (articleBean.getTransmit_num() != null && !TextUtils.isEmpty(articleBean.getTransmit_num())){
                ((ViewHolder2) viewHolder).tv_share.setText(articleBean.getTransmit_num());
            }

            if (articleBean.getComment_num() != null && !TextUtils.isEmpty(articleBean.getComment_num())){
                ((ViewHolder2) viewHolder).tv_comment.setText(articleBean.getComment_num());
            }

            if (articleBean.getNice_num() != null && !TextUtils.isEmpty(articleBean.getNice_num())){
                ((ViewHolder2) viewHolder).tv_like.setText(articleBean.getNice_num());
            }

            if (articleBean.getCover() != null){
                List<String> cover = articleBean.getCover();
                if (cover.size() > 1){
                    ((ViewHolder2) viewHolder).layout_cover1.setVisibility(View.GONE);
                    ((ViewHolder2) viewHolder).layout_cover2.setVisibility(View.VISIBLE);
                    ((ViewHolder2) viewHolder).riv_cover3.setVisibility(View.GONE);
                    Picasso.with(context)
                            .load(cover.get(0))
                            .error(R.drawable.img_default)
                            .into(((ViewHolder2) viewHolder).riv_cover1);
                    Picasso.with(context)
                            .load(cover.get(1))
                            .error(R.drawable.img_default)
                            .into(((ViewHolder2) viewHolder).riv_cover2);
                    if (cover.size() > 2){
                        ((ViewHolder2) viewHolder).riv_cover3.setVisibility(View.VISIBLE);
                        Picasso.with(context)
                                .load(cover.get(2))
                                .error(R.drawable.img_default)
                                .into(((ViewHolder2) viewHolder).riv_cover3);
                    }

                }else {
                    ((ViewHolder2) viewHolder).layout_cover1.setVisibility(View.VISIBLE);
                    ((ViewHolder2) viewHolder).layout_cover2.setVisibility(View.GONE);
                    Picasso.with(context)
                            .load(cover.get(0))
                            .error(R.drawable.img_default)
                            .transform(new RangleTransForm(20))
                            .into(((ViewHolder2) viewHolder).cover_big);
                }
            }

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void updateData(ArrayList<IndexBean.ArticleBean> dataBean) {
        this.list = dataBean;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        if (mOnItemClickListener != null){
            switch (v.getId()){
                case R.id.title:
                    mOnItemClickListener.onClick(v, ViewName.TITLE,position);
                    break;
                    default:
                        mOnItemClickListener.onClick(v, ViewName.ITEM,position);
                        break;
            }
        }
    }

    public void setPlay(int playPosition) {
        this.play = playPosition;
        notifyDataSetChanged();
    }


    public class ViewHolder1 extends RecyclerView.ViewHolder{
        public TxVideoPlayerController mController;

        @BindView(R.id.article1_video_player)
        NiceVideoPlayer player;
        @BindView(R.id.re)
        RelativeLayout layout_re;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.tv_share)
        TextView tv_share;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.tv_like)
        TextView tv_like;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            //itemView.setOnClickListener(IndexArticleAdapter.this);
            title.setOnClickListener(IndexArticleAdapter.this);
        }

        public void setController(TxVideoPlayerController controller) {
            mController = controller;
            player.setController(mController);
        }

        public void bindData(IndexBean.ArticleBean data) {
            mController.setTitle(data.getTitle());
            //mController.setLenght(data.getVideo_duration());
            Picasso.with(itemView.getContext())
                    .load(data.getBackground_cover())
                    //.placeholder(R.drawable.img_default)
                    //.crossFade()
                    .into(mController.imageView());
            player.setUp(data.getVideo_url(), null);
        }

    }

    public class ViewHolder2 extends RecyclerView.ViewHolder{

        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.layout_cover1)
        LinearLayout layout_cover1;
        @BindView(R.id.cover_big)
        ImageView cover_big;
        @BindView(R.id.layout_cover2)
        LinearLayout layout_cover2;
        @BindView(R.id.riv_cover1)
        ImageView riv_cover1;
        @BindView(R.id.riv_cover2)
        ImageView riv_cover2;
        @BindView(R.id.riv_cover3)
        ImageView riv_cover3;
        @BindView(R.id.tv_share)
        TextView tv_share;
        @BindView(R.id.tv_comment)
        TextView tv_comment;
        @BindView(R.id.tv_like)
        TextView tv_like;

        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(IndexArticleAdapter.this);
        }
    }

    public interface OnItemClickListener {
        void onClick(View view, ViewName viewName, int position);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.mOnItemClickListener=onItemClickListener;
    }

    public enum ViewName{
        ITEM,
        TITLE

    }

}
